package org.bukkit.pm.arena;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerMoveEvent;

public class ArenaPlayerListener extends PlayerListener {

	private ArenaPlugin _plugin;
	
	public ArenaPlayerListener(ArenaPlugin plugin) {
		_plugin = plugin;
	}
	
	public void onPlayerMove(PlayerMoveEvent event){
		Player p = event.getPlayer();
		if(!_plugin.checkBounds(p)){
			_plugin.printWarning(p);
			p.teleportTo(event.getFrom());
			event.setCancelled(true);
		}
	}
	
	public void onPlayerJoin(PlayerEvent event){
		//check login position
		if(!_plugin.checkBounds(event.getPlayer())){
			_plugin.forceBack(event.getPlayer()); //teleport to random location
		}
	}
	
	public void onPlayerTeleport(PlayerMoveEvent event){
		final Player p = event.getPlayer();
		if(p.getHealth()<=0){
			//respawn inside the arena
			p.setHealth(20); 
			final Location l = _plugin.getSpawnLocation(p);
			p.teleportTo(l);
			//prevent further notifications
			event.setCancelled(true); 
		}
	}
	
	public void onPlayerCommand(PlayerChatEvent event){
		//TODO: size 
		//TODO: center
		//TODO: info
		//TODO: tptest
	}
}
